package gui.gui2d.menu;

import gui.gui2d.highLevelPanels.PanelBackgraund;
import gui.gui2d.menu.events.MenuActionListener;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JWindow;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

import utility.NameMemmory;

import com.sun.awt.AWTUtilities;

public class JPanelSelection  extends JPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final int LEFT_PANEL_SIZE = 500;
	private static final int RIGHT_PANEL_SIZE = 500;
	private static final int TOP_PANEL_SIZE = 300;
	private static final int BOTTOM_PANEL_SIZE = 300;
	JPanel top= new JPanel();
	JPanel bottom = new JPanel();
	JPanel left= new JPanel();
	JPanel right = new JPanel();
	JPanel center = new JPanel();

	JLabel startServerLabel= new JLabel("Attendi giocatore dalla rete");
	JLabel startLocalLabel= new JLabel("Gioca sulla stessa scacchiera");
	JLabel startIaLabel= new JLabel("Gioca contro il Pc");
	JLabel searchGameLabel= new JLabel("Cerca giocatore in rete");
	JPanel startServerPanel = new JPanel();
	JLabel exitLabel= new JLabel("Esci");

	String carattere = "TimesRoman";
	int plain = Font.ITALIC;
	int dimensioni = 60;
	int dimensionField=40;
	Font font = new Font(carattere, plain, dimensioni);
	Font fontField = new Font(carattere, plain, dimensionField);

	JLabel waitOpponentLabel = new JLabel("In attesa di un avversario    ");
	private boolean enableAnimation=true;
	
	
	JButton backButton= new JButton("Indietro");
	JButton connectButton= new JButton("Avvia");
	JButton backButtonOnConnection = new JButton("Annulla");

	JTextField nick = new JTextField();
	JLabel text = new JLabel("Immetti il tuo nick:");
	
	int dimensioni1 = 28;
	Font font1 = new Font(carattere, plain, dimensioni1);
	
	JTextField textFieldPort = new JTextField();
	
	public JPanelSelection() {

	}

	void loadComponents(){
		this.setLayout(new BorderLayout());
		this.setOpaque(false);
		top.setOpaque(false);
		bottom.setOpaque(false);
		left.setOpaque(false);
		right.setOpaque(false);

		center.setBorder(BorderFactory.createMatteBorder( 6, 6, 6, 6 , new Color(101,67,33)));
		center.setOpaque(true);
		center.setBackground(new Color(80,50,50,100));

		//center.setBorder(BorderFactory.createLineBorder(new Color(101,67,33)));


		this.add(top , BorderLayout.NORTH);
		this.add(bottom, BorderLayout.SOUTH);
		this.add(left, BorderLayout.WEST);
		this.add(right, BorderLayout.EAST);
		this.add(center, BorderLayout.CENTER);


		left.setPreferredSize(new Dimension(LEFT_PANEL_SIZE,0));
		right.setPreferredSize(new Dimension(RIGHT_PANEL_SIZE,0));
		top.setPreferredSize(new Dimension(0,TOP_PANEL_SIZE));
		bottom.setPreferredSize(new Dimension(0, BOTTOM_PANEL_SIZE));


	}



	void start(){
		center.setLayout(new FlowLayout());
		//center.setBackground(Color.BLUE);

		loadMenu();
	}



	public  void loadMenu(){
		removeText();
		center.setLayout(new BoxLayout(center, BoxLayout.PAGE_AXIS));
		
		
		JPanel startIAGamePanel = new JPanel();
		startIAGamePanel.setOpaque(false);
		startIAGamePanel.setBackground(Color.black);
		startIAGamePanel.setLayout(new FlowLayout());
		center.add(startIAGamePanel);
		startIAGamePanel.add(startIaLabel);
		startIaLabel.setForeground(Color.WHITE);
		startIaLabel.setFont(font);
		startIaLabel.setMinimumSize(new Dimension(100,100));
		

		JPanel startLocalGamePanel = new JPanel();
		startLocalGamePanel.setOpaque(false);
		startLocalGamePanel.setBackground(Color.black);
		startLocalGamePanel.setLayout(new FlowLayout());
		center.add(startLocalGamePanel);



		startLocalLabel.setForeground(Color.WHITE);
		startLocalLabel.setFont(font);
		startLocalLabel.setMinimumSize(new Dimension(100,100));
		startLocalGamePanel.add(startLocalLabel);







		startServerPanel.setOpaque(false);

		startServerPanel.setLayout(new FlowLayout());
		center.add(startServerPanel);








		startServerLabel.setForeground(Color.WHITE);
		startServerLabel.setFont(font);
		startServerLabel.setMinimumSize(new Dimension(100,100));
		startServerPanel.add(startServerLabel);


		JPanel searchGamePanel = new JPanel();
		searchGamePanel.setOpaque(false);
		searchGamePanel.setLayout(new FlowLayout());
		center.add(searchGamePanel);


		searchGameLabel.setForeground(Color.WHITE);
		searchGameLabel.setFont(font);
		searchGameLabel.setMinimumSize(new Dimension(100,100));
		searchGamePanel.add(searchGameLabel);


		JPanel exitPanel = new JPanel();
		exitPanel.setOpaque(false);
		exitPanel.setLayout(new FlowLayout());
		center.add(exitPanel);
		
		exitLabel.setForeground(Color.WHITE);
		exitLabel.setFont(font);
		exitLabel.setMinimumSize(new Dimension(100,100));
		exitPanel.add(exitLabel);


	}





	public static void main(String[] args) {
		JFrame jf = new JFrame();
		jf.setUndecorated(true);
		GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0];
		device.setFullScreenWindow(jf);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		PanelBackgraund pbg= new PanelBackgraund();
		pbg.setLayout(new BorderLayout());
		jf.add(pbg,BorderLayout.CENTER);
		JPanelSelection jps= new JPanelSelection();
		jps.loadComponents();
		jps.start();

		pbg.add(jps, BorderLayout.CENTER);
		jf.setVisible(true);

	}
	
	public void createIAListener(MouseListener al){
		startIaLabel.addMouseListener(al);
	}

	public void createServerListener(MouseListener al){
		startServerLabel.addMouseListener(al);
	}

	public void searchNewGameOnLanListener(MouseListener al){
		searchGameLabel.addMouseListener(al);
	}

	public void createNewLocalGameListener(MouseListener al){
		//newLocalGame.addActionListener(al);
		startLocalLabel.addMouseListener(al);
	}

	public void exitListener(MouseListener al){
		exitLabel.addMouseListener(al);
	}

	@Override
	public void paintComponents(Graphics g) {
		// TODO Auto-generated method stub
		super.paintComponents(g);
	}

	public void removeText() {
		Component c[] = center.getComponents();
		for(int i=0;i<c.length;i++){
			center.remove(c[i]);
		}
		repaint();
		
	}
	
	
	public void addNickEditPanel(){
		JPanel topPanel = new JPanel();
		JPanel middlePAnel = new JPanel();
		JPanel bottomPanel = new JPanel();
		topPanel.setOpaque(false);
		middlePAnel.setOpaque(false);
		bottomPanel.setOpaque(false);
		center.add(topPanel);
		topPanel.setLayout(new FlowLayout());
		
		text.setForeground(Color.WHITE);
		text.setFont(font);
		topPanel.add(text);
		JTextField jf= new JTextField();
		jf.setPreferredSize(new Dimension(100,70));
		
		
		center.add(middlePAnel);
		middlePAnel.setLayout(new FlowLayout());
		nick.setText(NameMemmory.getInstace().getName());
		nick.setPreferredSize(new Dimension(300,55));
		nick.setFont(fontField);
		
		
		//middlePAnel.add(nick);
		
		
		JLabel jlabelPort= new JLabel("Porta server:");
		jlabelPort.setFont(font);
		jlabelPort.setForeground(Color.WHITE);
		
		
		textFieldPort.setPreferredSize(new Dimension(150,55));
		textFieldPort.setFont(fontField);
		textFieldPort.setText("8457");
		
		topPanel.add(nick);
		
		
		
		middlePAnel.add(jlabelPort);
		middlePAnel.add(textFieldPort);
		
		
		center.add(bottomPanel);
		bottomPanel.setLayout(new FlowLayout());
		
		connectButton.setFont(font1);
		connectButton.setOpaque(false);
		connectButton.setPreferredSize(new Dimension(150,40));
		backButton.setFont(font1);
		backButton.setOpaque(false);
		backButton.setPreferredSize(new Dimension(150,40));
		bottomPanel.add(backButton);
		bottomPanel.add(connectButton);
	
	}
	
	public void addServerTexts(){
		JPanel startLocalGamePanel = new JPanel();
		startLocalGamePanel.setOpaque(false);
		startLocalGamePanel.setLayout(new FlowLayout());
		center.add(startLocalGamePanel);
		
		JPanel back= new JPanel();
		back.setOpaque(false);
		back.add(backButtonOnConnection);
		center.add(back);
		backButtonOnConnection.setFont(font1);
		backButtonOnConnection.setPreferredSize(new Dimension(150,40));
		waitOpponentLabel.setForeground(Color.WHITE);
		waitOpponentLabel.setFont(font);
		waitOpponentLabel.setMinimumSize(new Dimension(100,100));
		startLocalGamePanel.add(waitOpponentLabel);
		startAnimation();
	}
	
	
	void startAnimation(){
		Thread t= new Thread(new Runnable() {
			
			@Override
			public void run() {
				while(enableAnimation){
					waitOpponentLabel.setText("In attesa di un avversario    ");
					repaint();
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					waitOpponentLabel.setText("In attesa di un avversario.   ");
					repaint();
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					waitOpponentLabel.setText("In attesa di un avversario..  ");
					repaint();
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					waitOpponentLabel.setText("In attesa di un avversario... ");
					repaint();
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					waitOpponentLabel.setText("In attesa di un avversario....");
					repaint();
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				
			}
		});
		
		t.start();
	}
	public void addListenerConnectButton(MouseListener ml){
		connectButton.addMouseListener(ml);
	}
	
	public void addListenerBackButton(MouseListener ml){
		backButton.addMouseListener(ml);
	}
	
	public void addListenerBackButtonOnConnection(MouseListener ml){
		backButtonOnConnection.addMouseListener(ml);
	}
	
	
	public String getNickServerPlayer(){
		return nick.getText();
	}
	
	public void animateErrorNick(){
		Thread t = new Thread(new Runnable() {
			
			@Override
			public void run() {
				repaint();
				text.setText(" ");
				repaint();
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				text.setText("Immetti il tuo nick:");
				repaint();
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				text.setText(" ");
				repaint();
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				text.setText("Immetti il tuo nick:");
				repaint();
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		t.start();
	}

	public String getPortServerPlayer() {
		return textFieldPort.getText();
		
	}
	
}

